//
//  ViewController.swift
//  Enum实践
//
//  Created by 我演示 on 2020/5/12.
//  Copyright © 2020 Jerry-Fu. All rights reserved.
//


/**
 枚举声明的类型是囊括可能状态的有限集，且可以具有附加值。通过内嵌(nesting),方法(method),关联值(associated values)和模式匹配(pattern matching),枚举可以分层次地定义任何有组织的数据。
。*/

import UIKit

// 进行基本的控制操作
public enum Movement : Int{
    case Left = 0  // 可以添加初始值
    case Right = 1
    case Top = 2
    case Bottom = 3
}
//字符串添加关联值

/**整型(Integer)
浮点数(Float Point)
字符串(String)
布尔类型(Boolean)*/
enum House: String {
    case Baratheon = "Ours is the Fury"
    case Greyjoy = "We Do Not Sow"
    case Martell = "Unbowed, Unbent, Unbroken"
    case Stark = "Winter is Coming"
    case Tully = "Family, Duty, Honor"
    case Tyrell = "Growing Strong"
}


// 3 :嵌套枚举(Nesting Enums)

enum Character {
  enum Weapon {
    case Bow
    case Sword
    case Lance
    case Dagger
  }
  enum Helmet {
    case Wooden
    case Iron
    case Diamond
  }
  case Thief
  case Warrior
  case Knight
}

//4 可以使用struct进行嵌套枚举类型

struct CharacterFour {
   enum CharacterType {
    case Thief
    case Warrior
    case Knight
  }
  enum Weapon {
    case Bow
    case Sword
    case Lance
    case Dagger
  }
  let type: CharacterType
  let weapon: Weapon
}


//5 ： 模式匹配和关联知
// 5.1 股票的买和卖，然后数量和股票代码
enum Trade {
    case Buy
    case Sell
}
func trade(tradeType: Trade, stock: String, amount: Int) {}

//但是直接可以进 行改变 - 为模式匹配，用来匹配数据传输，或者回传 在一个请求类中可以使用
//对应的模式匹配，
enum TradeSendSecond  {
    case Buy(stock: String , amount : Int )
    case Sell(stock: String , amount : Int )
}
func tradeSendSecond(type: TradeSendSecond){}

// 5 ： d定义一个元祖类型的数据
typealias Config = (RAM: Int, CPU: String, GPU: String)

// 5.1 简单的面向函数样式的编程
func selectRAM(_ config: Config) -> Config {return (RAM: 32, CPU: config.CPU, GPU: config.GPU)}
func selectCPU(_ config: Config) -> Config {return (RAM: config.RAM, CPU: "3.2GHZ", GPU: config.GPU)}
func selectGPU(_ config: Config) -> Config {return (RAM: config.RAM, CPU: "3.2GHZ", GPU: "NVidia")}
enum Desktop {
   case Cube(Config)
   case Tower(Config)
   case Rack(Config)
}
let aTower = Desktop.Tower(selectCPU(selectGPU(selectRAM((0 , "sg" , "12")))))




class ViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.testFirst()
        
        //2 模式匹配 2
        self.testSecond()
        
    }
    
}

//1 初级用法
extension ViewController  {
 
    func testFirst() {
        //多模式匹配模式
        let aMoveMent = Movement.Left
        switch aMoveMent {
            case .Left : print("left")

                break
            default:()
                
            }
         
        let houseMent = House.Baratheon
        switch houseMent {
        case .Baratheon:
            print(houseMent.rawValue)
        default:
            break
        }
        
        //嵌套枚举的类型
        let character = Character.Thief
        let weapon = Character.Weapon.Bow
        let helmet = Character.Helmet.Iron
        
        // 4 :嵌套枚举类型
        let charactFour = CharacterFour(type: CharacterFour.CharacterType.Warrior, weapon: .Lance)
        
    }
    
    //模式匹配
    func testSecond () {
        let tradeSecond = TradeSendSecond.Buy(stock: "apple", amount: 500)
     
        
        if case let  TradeSendSecond.Buy(stock, amount) = tradeSecond {
            print("nskls")
        }
        
        
    }

    
    
}

